#include "bsp_gpio.h"
#include "gd32a508_gpio.h"
#include "main.h"
#include "bsp.h"

#define LED_CTRL_PORT GPIOB
#define LED_CTRL_PIN GPIO_PIN_9
#define PW_CTRL_PORT GPIOC
#define PW_CTRL_PIN GPIO_PIN_13

#define BP_CTRL_PORT GPIOE
#define BP_CTRL_PIN GPIO_PIN_0
#define LP_CTRL_PORT GPIOC
#define LP_CTRL_PIN GPIO_PIN_8

#define ON_ST_PORT GPIOC
#define ON_ST_PIN GPIO_PIN_14
#define CHG_ST_PORT GPIOC
#define CHG_ST_PIN GPIO_PIN_15
#define CC2_ST_PORT GPIOE
#define CC2_ST_PIN GPIO_PIN_4

#define FB1_ST_PORT GPIOB
#define FB2_ST_PORT GPIOB
#define FB3_ST_PORT GPIOB
#define FB4_ST_PORT GPIOB
#define FB5_ST_PORT GPIOD
#define FB6_ST_PORT GPIOD
#define FB7_ST_PORT GPIOD
#define FB8_ST_PORT GPIOD
#define FB9_ST_PORT GPIOD
#define FB10_ST_PORT GPIOD
#define FB11_ST_PORT GPIOD
#define FB12_ST_PORT GPIOD

#define FB1_ST_PIN GPIO_PIN_12
#define FB2_ST_PIN GPIO_PIN_13
#define FB3_ST_PIN GPIO_PIN_14
#define FB4_ST_PIN GPIO_PIN_15
#define FB5_ST_PIN GPIO_PIN_8
#define FB6_ST_PIN GPIO_PIN_9
#define FB7_ST_PIN GPIO_PIN_10
#define FB8_ST_PIN GPIO_PIN_11
#define FB9_ST_PIN GPIO_PIN_12
#define FB10_ST_PIN GPIO_PIN_13
#define FB11_ST_PIN GPIO_PIN_14
#define FB12_ST_PIN GPIO_PIN_15

#define SW1_CTRL_PORT GPIOB
#define SW2_CTRL_PORT GPIOB
#define SW3_CTRL_PORT GPIOE
#define SW4_CTRL_PORT GPIOE
#define SW5_CTRL_PORT GPIOE
#define SW6_CTRL_PORT GPIOE
#define SW7_CTRL_PORT GPIOE
#define SW8_CTRL_PORT GPIOE
#define SW9_CTRL_PORT GPIOE
#define SW10_CTRL_PORT GPIOE
#define SW11_CTRL_PORT GPIOE
#define SW12_CTRL_PORT GPIOB
#define USART_RX_EN_PORT GPIOC

#define SW1_CTRL_PIN GPIO_PIN_11
#define SW2_CTRL_PIN GPIO_PIN_10
#define SW3_CTRL_PIN GPIO_PIN_15
#define SW4_CTRL_PIN GPIO_PIN_14
#define SW5_CTRL_PIN GPIO_PIN_13
#define SW6_CTRL_PIN GPIO_PIN_12
#define SW7_CTRL_PIN GPIO_PIN_11
#define SW8_CTRL_PIN GPIO_PIN_10
#define SW9_CTRL_PIN GPIO_PIN_9
#define SW10_CTRL_PIN GPIO_PIN_8
#define SW11_CTRL_PIN GPIO_PIN_7
#define SW12_CTRL_PIN GPIO_PIN_2
#define USART_RX_EN_PIN GPIO_PIN_12

dv_DIDO_TypeDef dv_DIDO;
static osTimeCost_T pmain[2] = {0};

static void bsp_gpio_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_GPIOE);

	gpio_init(BP_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, BP_CTRL_PIN);
	gpio_init(LP_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LP_CTRL_PIN);
	gpio_init(LED_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_CTRL_PIN);
	gpio_init(PW_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, PW_CTRL_PIN);
	gpio_init(SW1_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW1_CTRL_PIN);
	gpio_init(SW2_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW2_CTRL_PIN);
	gpio_init(SW3_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW3_CTRL_PIN);
	gpio_init(SW4_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW4_CTRL_PIN);
	gpio_init(SW5_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW5_CTRL_PIN);
	gpio_init(SW6_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW6_CTRL_PIN);
	gpio_init(SW7_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW7_CTRL_PIN);
	gpio_init(SW8_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW8_CTRL_PIN);
	gpio_init(SW9_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW9_CTRL_PIN);
	gpio_init(SW10_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW10_CTRL_PIN);
	gpio_init(SW11_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW11_CTRL_PIN);
	gpio_init(SW12_CTRL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SW12_CTRL_PIN);
	gpio_init(USART_RX_EN_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, USART_RX_EN_PIN);

	GPIO_BC(BP_CTRL_PORT) = BP_CTRL_PIN;
	GPIO_BC(LP_CTRL_PORT) = LP_CTRL_PIN;
	GPIO_BOP(LED_CTRL_PORT) = LED_CTRL_PIN;
	GPIO_BOP(PW_CTRL_PORT) = PW_CTRL_PIN;
	GPIO_BC(SW1_CTRL_PORT) = SW1_CTRL_PIN;
	GPIO_BC(SW2_CTRL_PORT) = SW2_CTRL_PIN;
	GPIO_BC(SW3_CTRL_PORT) = SW3_CTRL_PIN;
	GPIO_BC(SW4_CTRL_PORT) = SW4_CTRL_PIN;
	GPIO_BC(SW5_CTRL_PORT) = SW5_CTRL_PIN;
	GPIO_BC(SW6_CTRL_PORT) = SW6_CTRL_PIN;
	GPIO_BC(SW7_CTRL_PORT) = SW7_CTRL_PIN;
	GPIO_BC(SW8_CTRL_PORT) = SW8_CTRL_PIN;
	GPIO_BC(SW9_CTRL_PORT) = SW9_CTRL_PIN;
	GPIO_BC(SW10_CTRL_PORT) = SW10_CTRL_PIN;
	GPIO_BC(SW11_CTRL_PORT) = SW11_CTRL_PIN;
	GPIO_BC(SW12_CTRL_PORT) = SW12_CTRL_PIN;
	GPIO_BC(USART_RX_EN_PORT) = USART_RX_EN_PIN;
	
	gpio_init(ON_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, ON_ST_PIN);
	gpio_init(CHG_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, CHG_ST_PIN);
	gpio_init(CC2_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, CC2_ST_PIN);
	gpio_init(FB1_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB1_ST_PIN);
	gpio_init(FB2_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB2_ST_PIN);
	gpio_init(FB3_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB3_ST_PIN);
	gpio_init(FB4_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB4_ST_PIN);
	gpio_init(FB5_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB5_ST_PIN);
	gpio_init(FB6_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB6_ST_PIN);
	gpio_init(FB7_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB7_ST_PIN);
	gpio_init(FB8_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB8_ST_PIN);
	gpio_init(FB9_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB9_ST_PIN);
	gpio_init(FB10_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB10_ST_PIN);
	gpio_init(FB11_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB11_ST_PIN);
	gpio_init(FB12_ST_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FB12_ST_PIN);		
}

static void bsp_gpio_ctrl(GPIO_PortDef port, bool leve)
{
	switch (port)
	{
	case IO_LED:
		(leve == true) ? (GPIO_BOP(LED_CTRL_PORT) = LED_CTRL_PIN) : (GPIO_BC(LED_CTRL_PORT) = LED_CTRL_PIN);
		break;
	case IO_PW:
		(leve == true) ? (GPIO_BOP(PW_CTRL_PORT) = PW_CTRL_PIN) : (GPIO_BC(PW_CTRL_PORT) = PW_CTRL_PIN);
		break;
	case IO_BP:
		(leve == true) ? (GPIO_BOP(BP_CTRL_PORT) = BP_CTRL_PIN) : (GPIO_BC(BP_CTRL_PORT) = BP_CTRL_PIN);
		break;
	case IO_LP:
		(leve == true) ? (GPIO_BOP(LP_CTRL_PORT) = LP_CTRL_PIN) : (GPIO_BC(LP_CTRL_PORT) = LP_CTRL_PIN);
		break;
	case IO_SW1:
		(leve == true) ? (GPIO_BOP(SW1_CTRL_PORT) = SW1_CTRL_PIN) : (GPIO_BC(SW1_CTRL_PORT) = SW1_CTRL_PIN);
		break;
	case IO_SW2:
		(leve == true) ? (GPIO_BOP(SW2_CTRL_PORT) = SW2_CTRL_PIN) : (GPIO_BC(SW2_CTRL_PORT) = SW2_CTRL_PIN);
		break;
	case IO_SW3:
		(leve == true) ? (GPIO_BOP(SW3_CTRL_PORT) = SW3_CTRL_PIN) : (GPIO_BC(SW3_CTRL_PORT) = SW3_CTRL_PIN);
		break;
	case IO_SW4:
		(leve == true) ? (GPIO_BOP(SW4_CTRL_PORT) = SW4_CTRL_PIN) : (GPIO_BC(SW4_CTRL_PORT) = SW4_CTRL_PIN);
		break;
	case IO_SW5:
		(leve == true) ? (GPIO_BOP(SW5_CTRL_PORT) = SW5_CTRL_PIN) : (GPIO_BC(SW5_CTRL_PORT) = SW5_CTRL_PIN);
		break;
	case IO_SW6:
		(leve == true) ? (GPIO_BOP(SW6_CTRL_PORT) = SW6_CTRL_PIN) : (GPIO_BC(SW6_CTRL_PORT) = SW6_CTRL_PIN);
		break;
	case IO_SW7:
		(leve == true) ? (GPIO_BOP(SW7_CTRL_PORT) = SW7_CTRL_PIN) : (GPIO_BC(SW7_CTRL_PORT) = SW7_CTRL_PIN);
		break;
	case IO_SW8:
		(leve == true) ? (GPIO_BOP(SW8_CTRL_PORT) = SW8_CTRL_PIN) : (GPIO_BC(SW8_CTRL_PORT) = SW8_CTRL_PIN);
		break;
	case IO_SW9:
		(leve == true) ? (GPIO_BOP(SW9_CTRL_PORT) = SW9_CTRL_PIN) : (GPIO_BC(SW9_CTRL_PORT) = SW9_CTRL_PIN);
		break;
	case IO_SW10:
		(leve == true) ? (GPIO_BOP(SW10_CTRL_PORT) = SW10_CTRL_PIN) : (GPIO_BC(SW10_CTRL_PORT) = SW10_CTRL_PIN);
		break;
	case IO_SW11:
		(leve == true) ? (GPIO_BOP(SW11_CTRL_PORT) = SW11_CTRL_PIN) : (GPIO_BC(SW11_CTRL_PORT) = SW11_CTRL_PIN);
		break;
	case IO_SW12:
		(leve == true) ? (GPIO_BOP(SW12_CTRL_PORT) = SW12_CTRL_PIN) : (GPIO_BC(SW12_CTRL_PORT) = SW12_CTRL_PIN);
		break;
	case IO_USART_RX_EN:
		(leve == true) ? (GPIO_BOP(USART_RX_EN_PORT) = USART_RX_EN_PIN) : (GPIO_BC(USART_RX_EN_PORT) = USART_RX_EN_PIN);
		break;
	default:
		break;
	}
}

static bool bsp_gpio_state(GPIO_PortDef port)
{
	bool state = false;

	switch (port)
	{
	case IO_PW:
		((GPIO_ISTAT(PW_CTRL_PORT) & (PW_CTRL_PIN))) ? (state = true) : (state = false);
		
		break;
	case IO_BP:
		((GPIO_ISTAT(BP_CTRL_PORT) & (BP_CTRL_PIN))) ? (state = true) : (state = false);
		break;
	case IO_LP:
		((GPIO_ISTAT(LP_CTRL_PORT) & (LP_CTRL_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB_ON:
		((GPIO_ISTAT(ON_ST_PORT) & (ON_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB_CHG:
		((GPIO_ISTAT(CHG_ST_PORT) & (CHG_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB_CC2:
		((GPIO_ISTAT(CC2_ST_PORT) & (CC2_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB1:
		((GPIO_ISTAT(FB1_ST_PORT) & (FB1_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB2:
		((GPIO_ISTAT(FB2_ST_PORT) & (FB2_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB3:
		((GPIO_ISTAT(FB3_ST_PORT) & (FB3_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB4:
		((GPIO_ISTAT(FB4_ST_PORT) & (FB4_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB5:
		((GPIO_ISTAT(FB5_ST_PORT) & (FB5_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB6:
		((GPIO_ISTAT(FB6_ST_PORT) & (FB6_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB7:
		((GPIO_ISTAT(FB7_ST_PORT) & (FB7_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB8:
		((GPIO_ISTAT(FB8_ST_PORT) & (FB8_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB9:
		((GPIO_ISTAT(FB9_ST_PORT) & (FB9_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB10:
		((GPIO_ISTAT(FB10_ST_PORT) & (FB10_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB11:
		((GPIO_ISTAT(FB11_ST_PORT) & (FB11_ST_PIN))) ? (state = true) : (state = false);
		break;
	case IO_FB12:
		((GPIO_ISTAT(FB12_ST_PORT) & (FB12_ST_PIN))) ? (state = true) : (state = false);
		break;
	default:
		break;
	}

	return state;
}

static void bsp_gpio_trigger(GPIO_PortDef port)
{
	static bool leve = false;

	switch (port)
	{
	case IO_LED:
		(leve == true) ? (GPIO_BOP(LED_CTRL_PORT) = LED_CTRL_PIN) : (GPIO_BC(LED_CTRL_PORT) = LED_CTRL_PIN);
		break;
	case IO_PW:
		(leve == true) ? (GPIO_BOP(PW_CTRL_PORT) = PW_CTRL_PIN) : (GPIO_BC(PW_CTRL_PORT) = PW_CTRL_PIN);
		break;
	case IO_SW1:
		(leve == true) ? (GPIO_BOP(SW1_CTRL_PORT) = SW1_CTRL_PIN) : (GPIO_BC(SW1_CTRL_PORT) = SW1_CTRL_PIN);
		break;
	case IO_SW2:
		(leve == true) ? (GPIO_BOP(SW2_CTRL_PORT) = SW2_CTRL_PIN) : (GPIO_BC(SW2_CTRL_PORT) = SW2_CTRL_PIN);
		break;
	case IO_SW3:
		(leve == true) ? (GPIO_BOP(SW3_CTRL_PORT) = SW3_CTRL_PIN) : (GPIO_BC(SW3_CTRL_PORT) = SW3_CTRL_PIN);
		break;
	case IO_SW4:
		(leve == true) ? (GPIO_BOP(SW4_CTRL_PORT) = SW4_CTRL_PIN) : (GPIO_BC(SW4_CTRL_PORT) = SW4_CTRL_PIN);
		break;
	case IO_SW5:
		(leve == true) ? (GPIO_BOP(SW5_CTRL_PORT) = SW5_CTRL_PIN) : (GPIO_BC(SW5_CTRL_PORT) = SW5_CTRL_PIN);
		break;
	case IO_SW6:
		(leve == true) ? (GPIO_BOP(SW6_CTRL_PORT) = SW6_CTRL_PIN) : (GPIO_BC(SW6_CTRL_PORT) = SW6_CTRL_PIN);
		break;
	case IO_SW7:
		(leve == true) ? (GPIO_BOP(SW7_CTRL_PORT) = SW7_CTRL_PIN) : (GPIO_BC(SW7_CTRL_PORT) = SW7_CTRL_PIN);
		break;
	case IO_SW8:
		(leve == true) ? (GPIO_BOP(SW8_CTRL_PORT) = SW8_CTRL_PIN) : (GPIO_BC(SW8_CTRL_PORT) = SW8_CTRL_PIN);
		break;
	case IO_SW9:
		(leve == true) ? (GPIO_BOP(SW9_CTRL_PORT) = SW9_CTRL_PIN) : (GPIO_BC(SW9_CTRL_PORT) = SW9_CTRL_PIN);
		break;
	case IO_SW10:
		(leve == true) ? (GPIO_BOP(SW10_CTRL_PORT) = SW10_CTRL_PIN) : (GPIO_BC(SW10_CTRL_PORT) = SW10_CTRL_PIN);
		break;
	case IO_SW11:
		(leve == true) ? (GPIO_BOP(SW11_CTRL_PORT) = SW11_CTRL_PIN) : (GPIO_BC(SW11_CTRL_PORT) = SW11_CTRL_PIN);
		break;
	case IO_SW12:
		(leve == true) ? (GPIO_BOP(SW12_CTRL_PORT) = SW12_CTRL_PIN) : (GPIO_BC(SW12_CTRL_PORT) = SW12_CTRL_PIN);
		break;
	default:
		break;
	}

	leve = !leve;
}

static void led_spark(void)
{
	static uint16_t sparkfrq = 1000;

	switch (dv_DIDO.LEDCtrl.State)
	{
		case 0:
			sparkfrq = 1000;
			break;
		case 1:
			sparkfrq = 700;
			break;
		case 2:
			sparkfrq = 300;
			break;
		case 3:
			sparkfrq = 100;
			break;
		default:
			break;
	}

	if (dv_DIDO.LEDCtrl.Flag)
	{
		dv_DIDO.LEDCtrl.HCnt++;
	}
	else
	{
		dv_DIDO.LEDCtrl.LCnt++;
	}

	if (dv_DIDO.LEDCtrl.HCnt >= SYS_HEART_MS(sparkfrq))
	{
		dv_DIDO.LEDCtrl.HCnt = 0;
		dv_DIDO.LEDCtrl.Flag = false;//亮
		//osTaskGetTimeCost(&pmain[0]);
	}

	if (dv_DIDO.LEDCtrl.LCnt >= SYS_HEART_MS(sparkfrq))
	{
		dv_DIDO.LEDCtrl.LCnt = 0;
		dv_DIDO.LEDCtrl.Flag = true;//灭
	}

	bsp_gpio_ctrl(IO_LED, dv_DIDO.LEDCtrl.Flag);
}

void init_bsp_dido(void)
{
	dv_DIDO.finit     = bsp_gpio_init;
	dv_DIDO.fwrite    = bsp_gpio_ctrl;
	dv_DIDO.ftrigger  = bsp_gpio_trigger;
	dv_DIDO.fread     = bsp_gpio_state;
	dv_DIDO.fledspark = led_spark;
}